using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.ContactsDomain;
using ShivamEMR.Model.DataInterfaces.ContactsDataInterf;
using ShivamEMR.Model;
//using ShivamEMR.Web.Areas.Contacts.Models;

namespace ShivamEMR.Web.Areas.Contacts.Controllers
{   
    public class ContactController : Controller
    {
		private readonly IContactTypeRepository contacttypeRepository;
		private readonly IContactRepository contactRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ContactController()
        {
            contacttypeRepository = Registry.RepositoryFactory.GetContactTypeRepository();
            contactRepository = Registry.RepositoryFactory.GetContactRepository();
        }

        public ContactController(IContactTypeRepository contacttypeRepository, IContactRepository contactRepository)
        {
			this.contacttypeRepository = contacttypeRepository;
			this.contactRepository = contactRepository;
        }

        //
        // GET: /Contact/

        public ViewResult Index()
        {
            //return View(contactRepository.AllIncluding(contact => contact.ContactType, contact => contact.Persons));
            return View(contactRepository.All);
        }

        //
        // GET: /Contact/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(contactRepository.Find(id));
        }

        //
        // GET: /Contact/Create

        public ActionResult Create()
        {
			ViewBag.PossibleContactTypes = contacttypeRepository.All;
            return View();
        } 

        //
        // POST: /Contact/Create

        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            if (ModelState.IsValid) {
                contactRepository.InsertOrUpdate(contact);
                contactRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleContactTypes = contacttypeRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Contact/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleContactTypes = contacttypeRepository.All;
             return View(contactRepository.Find(id));
        }

        //
        // POST: /Contact/Edit/5

        [HttpPost]
        public ActionResult Edit(Contact contact)
        {
            if (ModelState.IsValid) {
                contactRepository.InsertOrUpdate(contact);
                contactRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleContactTypes = contacttypeRepository.All;
				return View();
			}
        }

        //
        // GET: /Contact/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(contactRepository.Find(id));
        }

        //
        // POST: /Contact/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var contact = contactRepository.Find(id);
            contactRepository.Delete(contact);
            contactRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

